package com.blue.core.util;

import java.io.PrintWriter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * JSON 工具类
 * 
 * @author zhengj
 * @since 1.0 2016年11月17日
 */
public class JsonUtil
{
	private static SerializerFeature[] serializer = new SerializerFeature[] {
			SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue,
			SerializerFeature.WriteClassName};
		
		private static SerializerFeature[] outstream = new SerializerFeature[] {
			SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue};
	
	private JsonUtil()
	{
	}
	
	/**
	 * 输出操作成功信息
	 * 
	 * @param pw 输出流
	 */
	public static void outSuccess(PrintWriter pw)
	{
		JSONObject map = new JSONObject();
		map.put("auth", true);
		map.put("session", true);
		map.put("state", true);
		try (PrintWriter p = pw)
		{
			p.print(map.toJSONString());
			p.flush();
		}
	}
	
	/**
	 * 输出操作成功信息
	 * 
	 * @param pw 输出流
	 * @param object 对象
	 */
	public static void outSuccess(PrintWriter pw, Object...object)
	{
		JSONObject map = new JSONObject();
		map.put("auth", true);
		map.put("session", true);
		map.put("state", true);
		for (int i = 0; i < object.length; i++)
		{
			map.put("content" + i, object[i]);
		}
		try (PrintWriter p = pw)
		{
			p.print(JSON.toJSONString(map, outstream));
			p.flush();
		}
	}
	
	/**
	 * 输出操作失败信息
	 * 
	 * @param pw 输出流
	 * @param msg 失败信息
	 */
	public static void outError(PrintWriter pw, String msg)
	{
		JSONObject map = new JSONObject();
		map.put("auth", true);
		map.put("session", true);
		map.put("state", false);
		map.put("msg", msg);
		try (PrintWriter p = pw)
		{
			p.print(map.toJSONString());
			p.flush();
		}
	}
	
	/**
	 * 输出操作失败信息：<pre>发生错误，请联系管理员。</pre>
	 * 
	 * @param pw 输出流
	 */
	public static void outError(PrintWriter pw)
	{
		JSONObject map = new JSONObject();
		map.put("auth", true);
		map.put("session", true);
		map.put("state", false);
		map.put("msg", "发生错误，请联系管理员。");
		try (PrintWriter p = pw)
		{
			p.print(map.toJSONString());
			p.flush();
		}
	}
	
	/**
	 * 输出JSON格式字符串
	 * 
	 * @param pw 输出流
	 * @param json JSON格式字符串
	 */
	public static void out(PrintWriter pw, String json)
	{
		try (PrintWriter p = pw)
		{
			p.print(json);
			p.flush();
		}
	}
	
	/**
	 * 把对象序列化成json格式字符串，带@type类型
	 * 
	 * @param object 对象
	 * @return json字符串
	 */
	public static String encode(Object object)
	{
		String json = JSON.toJSONString(object, serializer);
		return json;
	}
	
	/**
	 * 把对象序列化成json格式字符串
	 * 
	 * @param object 对象
	 * @return json字符串
	 */
	public static String output(Object object)
	{
		String json = JSON.toJSONString(object, outstream);
		return json;
	}
	
	/**
	 * 把json格式字符串反序列化成对象
	 * 
	 * @param <T> 对象类型
	 * @param json json字符器
	 * @param clazz 对象的类型
	 * @return 实例化的对象
	 */
	public static <T> T decode(String json, Class<T> clazz)
	{
		T obj = JSON.parseObject(json, clazz);
		return obj;
	}
	
}
